home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-19 / gpt32src.zip / APOLLO.TRM < prev    next >
Text File  |  1992-03-25  |  18KB  |  539 lines

  1. /*
  2.  * $Id: apollo.trm,v 3.26 92/03/24 22:34:58 woo Exp Locker: woo $
  3.  */
  4.  
  5. /* GNUPLOT - apollo.trm */
  6. /*
  7.     Apollo terminal driver for GNUplot.
  8.  
  9.     Open a pad for the graphics, and use GPR routines.  For additional
  10.     speed, we do the graphics to a separate bitmap, and the blt the
  11.     entire bitmap to the display.  When the user specifies an output
  12.     file name, however, we draw directly to the screen, so the graphics
  13.     are written to the file correctly.  Thus, the user can save the
  14.     graphics in a file, to be viewed later.  If we try the bitmap
  15.     trick, it looks funny.
  16.  
  17.     Ray Lischner (uunet!mntgfx!lisch)
  18.     4 October 1989    file created for GNUplot 1.1
  19.     26 March 1990    updated for GNUplot 2.0
  20.     30 October 1991    fixed minor problem in apollo_tic_sizes
  21. */
  22.  
  23. #include <apollo/base.h>
  24. #include <apollo/error.h>
  25. #include <apollo/pad.h>
  26. #include <apollo/gpr.h>
  27.  
  28. /* default tick sizes for small windows */
  29. #define APOLLO_VTIC     6
  30. #define APOLLO_HTIC     6    
  31.  
  32. /* issue an error message, using additional text "s" */
  33. #define apollo_error(s)    error_$print_name(status, (s), strlen(s))
  34.  
  35. /* if "status" indicates an error, then issue an error message */
  36. #define apollo_check(s)    if (status.all != status_$ok) apollo_error(s)
  37.  
  38. static ios_$id_t stream = -1;           /* the stream for the pad */
  39. static gpr_$bitmap_desc_t screen_desc;  /* the screen's bitmap */
  40. static gpr_$bitmap_desc_t bitmap_desc;  /* the graphics bitmap */
  41. static gpr_$attribute_desc_t attr;      /* attribute block for saved bitmap */
  42. static short draw_width;                /* default GPR draw width */
  43. static name_$long_pname_t font_name;    /* font path name */
  44. static int APOLLO_XMAX, APOLLO_YMAX;    /* window size */
  45. static boolean use_bitmap;              /* use a separate bitmap? */
  46.  
  47. /* return whether stdout is a DM pad */
  48. apollo_isa_pad()
  49. {
  50.     status_$t status;
  51.     pad_$isa(1, &status);
  52.     return (status.all == status_$ok);
  53. }
  54.  
  55. /*
  56.     Find out what the default font is for the pad, and save the
  57.     character height and width information.
  58.  
  59.     Note that we must save the font file name because we need
  60.     to reload the font file everytime the window changes size.
  61. */
  62. static void apollo_font_info(struct termentry* tbl, char* fname)
  63. {
  64.     short fwidth, fheight, flen;
  65.     status_$t status;
  66.  
  67.     /* get the font size & update the termentry table */
  68.     pad_$inq_font(stream, &fwidth, &fheight, fname, name_$long_pnamlen_max,
  69.           &flen, &status);
  70.     apollo_check("inq_font");
  71.     fname[flen] = '\0';
  72.  
  73.     tbl->v_char = fheight;
  74.     tbl->h_char = fwidth;
  75. }
  76.  
  77. /*
  78.     Initialize all the GPR stuff.  To save time, we draw into a separate
  79.     bitmap, and then blt it onto the screen all at once.  This results
  80.     in 5-10 times speed-up in the graphics, with only a little
  81.     complication.  Most of the complication is right here, making sure
  82.     we allocate the right bitmaps, etc., in the right order.  The rest
  83.     is in APOLLO_text(), where we actually BLT the bitmap onto the screen.
  84.     Everything else is the same.
  85.  
  86.     The bitmaps have the same size as the window.  If the window changes
  87.     size, then the bitmaps retain the same size, so the user sees part
  88.     of the plot or a lot of space around the plot.  Drawing a new plot,
  89.     or replotting the previous one causes APOLLO_graphics() to see if
  90.     the window has changed size, in which case the GPR is terminated,
  91.     and this routine is called again.  Thus, make sure any changes
  92.     preserve this ability.  Anything that should only be done once
  93.     to the pad should be handled by APOLLO_init().
  94.  
  95.     By the way, we save the current draw width, to be used later
  96.     for drawing extra wide lines.  This way we don't need to know
  97.     anything about the current output device characteristics;
  98.     we can just draw the default width, or twice the default width, etc.
  99. */
  100. static void apollo_gpr_init(struct termentry* tbl, pad_$window_desc_t* window)
  101. {
  102.     gpr_$offset_t size;
  103.     short fontid;
  104.     status_$t status;
  105.  
  106.     size.x_size = APOLLO_XMAX = tbl->xmax = window->width;
  107.     size.y_size = APOLLO_YMAX = tbl->ymax = window->height;
  108.  
  109.     /* now initialize GPR */
  110.     gpr_$init(gpr_$frame, stream, size, 1, &screen_desc, &status);
  111.     apollo_check("gpr_$init");
  112.  
  113.     if (use_bitmap)
  114.     {
  115.     /* allocate the bitmap and its attribute block */
  116.     gpr_$allocate_attribute_block(&attr, &status);
  117.     apollo_check("allocate_attribute_block");
  118.  
  119.     gpr_$allocate_bitmap(size, 1, attr, &bitmap_desc, &status);
  120.     apollo_check("allocate_bitmap");
  121.  
  122.     gpr_$set_bitmap(bitmap_desc, &status);
  123.     apollo_check("set_bitmap");
  124.     }
  125.  
  126.     /* set the font file */
  127.     gpr_$load_font_file(font_name, strlen(font_name), &fontid, &status);
  128.     apollo_check(font_name);
  129.  
  130.     gpr_$set_text_font(fontid, &status);
  131.     apollo_check("set_text_font");
  132.  
  133.     gpr_$inq_draw_width(&draw_width, &status);
  134.     apollo_check("inq_draw_width");
  135. }
  136.  
  137. /*
  138.     Determine the tick sizes to be used for labelling borders.
  139.     By default, we use 1/50 of the window size, which looks nice to me.
  140.     If this makes the ticks too small, however, we use a minimum
  141.     size, to make sure they are visible.  The minimum size was also
  142.     determined experimentally.
  143.  
  144.     Feel free to changes the sizes to something you feel looks better.
  145.  
  146.     This routine must be called after apollo_gpr_init(), because we
  147.     need to know the window size, as stored in the termentry table.
  148. */
  149. static void apollo_tic_sizes(struct termentry* tbl)
  150. {
  151.     /* base the tick size on the window size */
  152.     tbl->v_tic = tbl->ymax / 50;
  153.     if (tbl->v_tic < APOLLO_VTIC)
  154.     tbl->v_tic = APOLLO_VTIC;
  155.     tbl->h_tic = tbl->xmax / 50;
  156.     if (tbl->h_tic < APOLLO_HTIC)
  157.     tbl->h_tic = APOLLO_HTIC;
  158. }
  159.  
  160. /*
  161.     Terminate the GPR.  This is called whenever the window size
  162.     changes, and we need to reinitialize the GPR.  I assume that
  163.     calling gpr_$terminate() also deallocates the bitmaps and
  164.     attribute blocks because deallocating the screen's bitmap
  165.     causes terminate() to think GPR has already been terminated.
  166.  
  167.     Since this can be called many times, make sure nothing
  168.     drastic is done here, like closing the stream to the pad.
  169.     The only actions should be those that will be reinitialized
  170.     by apollo_gpr_init().
  171. */
  172. static void apollo_gpr_terminate()
  173. {
  174.     status_$t status;
  175.  
  176.     gpr_$terminate(false, &status);
  177.     apollo_check("terminate");
  178. }
  179.  
  180. /*
  181.     Initialize the graphics.  This is called once, so we do things
  182.     here that should be done exactly once, such as opening the window.
  183.     I like to give windows names, so it is obvious what the window's
  184.     contents are, but this causes a transcript to be kept in a file
  185.     whose name is the window's name.  This might be nice in some
  186.     circumstances, but to me it is a nuisance, so the file is
  187.     deleted immediately.  The name is unlikely to appear normally,
  188.     so there should be little interference with users' normal files.
  189.     If the user has explicitly set the output file, however, then
  190.     we use that name, and do not delete the file.  Thus, the
  191.     user can get a metafile of the plot.  We can tell if the
  192.     output file has been set because outstr is "STDOUT".  Otherwise,
  193.     outstr is the filename, in single quotes.  We need to strip
  194.     the quotes to make the file name.
  195.  
  196.     The DM defaults are used for window sizes and positions.  If
  197.     the user doesn't like it, he or she can change is and issue
  198.     a replot command (assuming a plot has already been generated).
  199.  
  200.     Note, also, that we must call pad_$set_scale() or else
  201.     pad_$inq_windows() returns scaled values, which is not what
  202.     we want.  Setting the scale to one (1) turns off the scaling,
  203.     so we get real pixel sizes.
  204.  
  205.     Finally, we get the name and size of the default font.  The
  206.     name is kept, as explained earlier.  Then we can initialize
  207.     the GPR stuff.
  208.  
  209.     Note that there is a way that APOLLO_init() gets called more
  210.     than once.  If the user issues the "set terminal apollo" command
  211.     more than once, then this is called, so we need to make sure
  212.     that we do not keep creating windows.
  213.  
  214.     An alternative strategy would be to interpret multiple "set term
  215.     apollo"s to mean create multiple windows.  The user could only
  216.     access the most recent window because GNUplot has no concept of
  217.     multiple terminals.  The user would, in fact, have no way of
  218.     removing old windows because they are still active.  We could try
  219.     catching keyboard events to see if the user presses EXIT, but I do
  220.     not want to start getting into that mess.  If the user really
  221.     wants this kind of functionality, then he or she can run gnuplot
  222.     multiple times.  I think that is a lot cleaner, don't you?
  223. */
  224. APOLLO_init()
  225. {
  226.     /* only initialize once */
  227.     if (stream == -1)
  228.     {
  229.     extern char outstr[];
  230.     struct termentry* tbl;
  231.     pad_$window_desc_t window;
  232.     name_$long_name_t wname;
  233.     short wnum;            /* junk needed by pad_$inq_windows() */
  234.     boolean unlink_wname;
  235.     status_$t status;
  236.  
  237.     tbl = &term_tbl[term];
  238.  
  239.     /* make the window name unique, with "gnuplot" in the label */
  240.     if (strcmp(outstr, "STDOUT") == 0)
  241.     {
  242.         sprintf(wname, "gnuplot-%d", getpid());
  243.         unlink_wname = true;
  244.     }
  245.     else
  246.     {
  247.         /* strip the single quotes around the file name */
  248.         strcpy(wname, outstr + 1);
  249.         wname[strlen(wname) - 1] = '\0';
  250.         unlink_wname = false;
  251.     }
  252.  
  253.     use_bitmap = unlink_wname;
  254.  
  255.     /* use the default window position and size */
  256.     window.top = window.left = window.width = window.height = 0;
  257.     pad_$create_window(wname, strlen(wname), pad_$transcript, 1, window,
  258.                &stream, &status);
  259.     apollo_check("create_window");
  260.  
  261.     /* if this is not the user's name, then delete the file */
  262.     if (unlink_wname)
  263.         unlink(wname);
  264.  
  265.     /* remove all scaling, to revert to pixel units, not char. units */
  266.     pad_$set_scale(stream, 1, 1, &status);
  267.     apollo_check("set_scale");
  268.  
  269.     /* get rid of the window when the program exits */
  270.     pad_$set_auto_close(stream, 1, true, &status);
  271.     apollo_check("set_auto_close");
  272.  
  273.     /* now determine the window size & update the termentry table */
  274.     pad_$inq_windows(stream, &window, 1, &wnum, &status);
  275.     apollo_check("inq_windows");
  276.  
  277.     /* the order of the next three calls is important */
  278.     apollo_font_info(tbl, font_name);
  279.     apollo_gpr_init(tbl, &window);
  280.     apollo_tic_sizes(tbl);
  281.     }
  282. }
  283.  
  284. /*
  285.     Prepare for graphics output.  Since this is what the user wants to
  286.     do when preparing a new plot, this is a meaningful time to see if
  287.     the window has changed size.  Thus, we avoid mucking about with
  288.     asynchronous traps, and we avoid the bigger problem of dealing
  289.     with a half-finished plot when the window changes size.
  290.  
  291.     Simply put, get the current window size, and if it has changed,
  292.     then get rid of the old bitmaps, etc., and allocate new ones at
  293.     the new size.  We also need to update the termentry table.
  294.     If the window stays the same size, then just clear it.
  295. */
  296. static void apollo_redo_window(pad_$window_desc_t* window)
  297. {
  298.     struct termentry* tbl = &term_tbl[term];
  299.     status_$t status;
  300.  
  301.     /* the order of the following calls is important */
  302.     apollo_gpr_terminate();
  303.     apollo_gpr_init(tbl, window);
  304.     apollo_tic_sizes(tbl);
  305. }
  306.  
  307. APOLLO_graphics()
  308. {
  309.     pad_$window_desc_t window;
  310.     short wnum;
  311.     status_$t status;
  312.  
  313.     pad_$inq_windows(stream, &window, 1, &wnum, &status);
  314.     apollo_check("inq_windows");
  315.  
  316.     if (window.width != APOLLO_XMAX || window.height != APOLLO_YMAX)
  317.     apollo_redo_window(&window);
  318.     else
  319.     {
  320.     gpr_$clear(0, &status);
  321.     apollo_check("clear");
  322.     }
  323. }
  324.  
  325. /* set a line type:
  326.    -2 heavy, solid    (border)
  327.    -1 heavy, dotted    (axis)
  328.    0  solid        (normal)
  329.    1  dots        (other curves)
  330.    2  short dash
  331.    3  long dash
  332.    4  dash dot
  333.  
  334.    Apparently, GPUplot draws a lot of short line segments, and each
  335.    one starts a new pattern.  This makes the patterns somewhat useless,
  336.    but one can still tell the difference between solid, dotted, and
  337.    dashed lines.  The utility of fancier styles is limited, however.
  338.  
  339.    On a color workstation, we should use different colors, but I
  340.    don't have one.
  341. */
  342.  
  343. /*
  344.     To draw different line styles on an Apollo, we use two different
  345.     parameters.  One is a line thickness, which is just an integral
  346.     multiple of the default line thickness.  The second is a 16-bit
  347.     pattern that is repeated.  We could use fancier patterns, since
  348.     GPR supports up to 64-bits, but, as I explained earlier, this
  349.     really does not buy us anything.
  350.  
  351.     I used patterns that do not start with all bits on because
  352.     GNUplot seems to use lots of short line segments to draw
  353.     a curve, and this might make a very curvey plot seem like
  354.     a solid line, regardless of pattern.  I don't want to start
  355.     with too many zeros, however, or else the curve might not
  356.     appear at all!  All the patterns, therefore, start with one
  357.     bit on.  The rest of the bits determine the true pattern.
  358.  
  359.     By examining graphics.c, we see that linetype -2 is used exclusively
  360.     for the border, -1 for the axes, and the non-negative integers for
  361.     the curves.  We use heavy lines for the border and axes, and normal
  362.     width lines for the curves.
  363.  
  364.     Since C arrays start at zero, make sure all the offsets are correct,
  365.     so that it is easy to access the array with -2...n linetypes.
  366. */
  367.  
  368. typedef struct {
  369.     short width;
  370.     short pattern;
  371. } APOLLO_LINE;
  372.  
  373. static APOLLO_LINE apollo_lines[] = {
  374.     { 2, ~0 },        /* heavy, solid */
  375.     { 2, 0x6666 },    /* heavy, dotted */
  376.     { 1, ~0 },        /* normal */
  377.     { 1, 0xAAAA },    /* dotted */
  378.     { 1, 0xC3C3 },    /* short dash */
  379.     { 1, 0xE01F },    /* long dash */
  380.     { 1, 0x87F8 },    /* dash dot */
  381.     { 1, 0x6666 },    /* big dots */
  382. };
  383.  
  384. #define BITS_PER_LINETYPE    16
  385.  
  386. /* apollo_line(-2) is the border style, etc. */
  387. #define apollo_line(x)        apollo_lines[(x)+2]
  388. #define apollo_pattern(x)    &apollo_line(x).pattern
  389. #define apollo_width(x)        apollo_line(x).width
  390.  
  391. #define APOLLO_MIN_LINE        (-2)
  392. #define APOLLO_MAX_LINE        (sizeof(apollo_lines)/sizeof(*apollo_lines)-2)
  393.  
  394. /* set the line style */
  395. APOLLO_linetype(ltype)
  396. int ltype;
  397. {
  398.     status_$t status;
  399.  
  400.     if (ltype < APOLLO_MIN_LINE)
  401.     ltype = APOLLO_MIN_LINE;
  402.     if (ltype >= APOLLO_MAX_LINE)
  403.     ltype %= APOLLO_MAX_LINE;
  404.  
  405.     gpr_$set_line_pattern(1, apollo_pattern(ltype), BITS_PER_LINETYPE, &status);
  406.     apollo_check("set_line_pattern");
  407.  
  408.     gpr_$set_draw_width(draw_width * apollo_width(ltype), &status);
  409.     apollo_check("set_draw_width");
  410. }
  411.  
  412. /* issue an error message that includes an (x, y) coordinate */
  413. static void apollo_xy_error(char* s, int x, int y, status_$t status)
  414. {
  415.     char buffer[128];
  416.  
  417.     sprintf(buffer, "%s(%d, %d)", s, x, y);
  418.     apollo_error(buffer);
  419. }
  420.  
  421. #define apollo_xy_check(s)    \
  422.     if (status.all != status_$ok) apollo_xy_error((s), x, y, status)
  423.  
  424. /*
  425.     Note that GNUplot and GPR have reversed ideas of where the Y origin is.
  426.     This means subtracting the Y coordinate from Y max.
  427. */
  428. #define plot_to_gpr(y)        (APOLLO_YMAX - (y))
  429.  
  430. /* move to a new position */
  431. APOLLO_move(unsigned int x, unsigned int y)
  432. {
  433.     status_$t status;
  434.  
  435.     gpr_$move((gpr_$coordinate_t) x, plot_to_gpr(y), &status);
  436.     apollo_xy_check("move");
  437. }
  438.  
  439. /* draw a line to a new position */
  440. APOLLO_vector(unsigned int x, unsigned int y)
  441. {
  442.     status_$t status;
  443.  
  444.     gpr_$line((gpr_$coordinate_t) x, plot_to_gpr(y), &status);
  445.     apollo_xy_check("line");
  446. }
  447.  
  448. /*
  449.     On terminals, this switches to text mode.  The real meaning,
  450.     however, is that the graphics are finished.  This means we can
  451.     now display the saved bitmap.
  452. */
  453. APOLLO_text()
  454. {
  455.     if (use_bitmap)
  456.     {
  457.     static gpr_$position_t pos;        /* always zero */
  458.     gpr_$window_t window;
  459.     status_$t status;
  460.  
  461.     /* bitblt the entire bitmap to the entire window */
  462.     window.window_base.x_coord = 0;
  463.     window.window_base.y_coord = 0;
  464.     window.window_size.x_size = APOLLO_XMAX;
  465.     window.window_size.y_size = APOLLO_YMAX;
  466.  
  467.     gpr_$set_bitmap(screen_desc, &status);
  468.     apollo_check("set_bitmap(screen_desc)");
  469.  
  470.     gpr_$pixel_blt(bitmap_desc, window, pos, &status);
  471.     apollo_check("bitblt");
  472.  
  473.     gpr_$set_bitmap(bitmap_desc, &status);
  474.     apollo_check("set_bitmap(bitmap_desc)");
  475.     }
  476. }
  477.  
  478. APOLLO_text_angle(ang)
  479. int ang;
  480. {
  481.     status_$t status;
  482.  
  483.     gpr_$set_text_path(ang ? gpr_$up : gpr_$right, &status);
  484.     apollo_check("set_text_path");
  485.     return TRUE;
  486. }
  487.  
  488. static enum JUSTIFY apollo_text_mode;
  489.  
  490. APOLLO_justify_text(mode)
  491. enum JUSTIFY mode;
  492. {
  493.     apollo_text_mode = mode;
  494.     return TRUE;
  495. }
  496.  
  497. /*
  498.     Write "str" right justified on row "row".  A row is assumed to
  499.     have whatever height the current text has.  Make sure the
  500.     text does not cover the tick marks.
  501. */
  502. APOLLO_put_text(x, y, str)
  503. unsigned int x, y;
  504. char str[];
  505. {
  506.     gpr_$offset_t size;
  507.     status_$t status;
  508.  
  509.     gpr_$inq_text_extent(str, strlen(str), &size, &status);
  510.     apollo_check("inq_text_extent");
  511.  
  512.     y -= size.y_size / 2;    /* center around "y" */
  513.     switch (apollo_text_mode)
  514.     {
  515.     case LEFT:
  516.     break;
  517.     case CENTRE:
  518.     x -= size.x_size / 2;
  519.     break;
  520.     case RIGHT:
  521.     x -= size.x_size;
  522.     break;
  523.     }
  524.     APOLLO_move(x, y);
  525.  
  526.     gpr_$text(str, strlen(str), &status);
  527.     apollo_check("put_text");
  528. }
  529.  
  530. /* reset the graphics state and terminate */
  531. APOLLO_reset()
  532. {
  533.     if (stream != -1)
  534.     {
  535.     apollo_gpr_terminate();
  536.     stream = -1;
  537.     }
  538. }
  539.